﻿using System;
using System.Configuration;
using System.Reflection;
using JTG.ServiceInterface.InterfaceService;

namespace JTG.ServiceInterface.Factory
{
    /// <summary>
    /// 签到服务工厂
    /// </summary>
    public class CheckInServiceFactory
    {
        private static string _iConfigReflection = ConfigurationManager.AppSettings["ConfigReflectionFunc"];
        //private static string _dllName = _iConfigReflection.Split(';')[0];
        private static string _appName = _iConfigReflection.Split(';')[1];

        /// <summary>
        /// 签到服务工厂
        /// </summary>
        /// <returns></returns>
        public ICheckInService CreateCheckInService()
        {
            //加载程序集
            var assembly = Assembly.Load("JTG.ServiceInterface");

            foreach (var t in assembly.GetTypes())
            {
                var customAttributes = t.GetCustomAttributes(typeof(Attribute.FunctionName), true);
                if (customAttributes.Length <= 0) continue;

                //查找【隆昌妇幼保健院签到机】的方法
                foreach (var attribute in ((Attribute.FunctionName[])customAttributes))
                {
                    if (!attribute.FcName.ToString().Equals(_appName)) continue;

                    if (t.FullName == null) return null;
                    var type = assembly.GetType(t.FullName);

                    return (ICheckInService)Activator.CreateInstance(type);
                }
            }

            return null;
        }
    }
}
